package Other;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Classname CodeMain
 * @Description TODO
 * @Date 2021/3/18 15:54
 * @Created by ASUS
 */
public class CodeMain {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(findBuilding(new int[]{5, 3, 8, 3, 2, 5})));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param str string字符串
     * @return string字符串
     */
    public static String compress(String str) {
        // write code here
        Stack<Character> stack_1 = new Stack<>();
        Stack<Character> stack_2 = new Stack<>();
        for (char c : str.toCharArray()) {
            stack_1.add(c);
        }
        while (!stack_1.isEmpty()) {
            char ch = stack_1.pop();
            if (ch != '[') {
                stack_2.push(ch);
            } else {
                StringBuilder sb = new StringBuilder();
                while (!stack_2.isEmpty() && stack_2.peek() != ']') {
                    sb.append(stack_2.pop());
                }
                if (!stack_2.isEmpty()) stack_2.pop();
                String helper = helper(sb.reverse().toString());
                for (char c : helper.toCharArray()) {
                    stack_2.push(c);
                }
            }
        }
        StringBuilder res = new StringBuilder();
        while (!stack_2.isEmpty()) res.append(stack_2.pop());
        return res.toString();

    }

    public static String helper(String str) {
        if (str.length() == 1) return str;
        String[] strings = str.split("\\|");
        StringBuilder tmp = new StringBuilder(strings[1]);
        System.out.println(Integer.parseInt(tmp.reverse().toString()));
        return String.valueOf(strings[0]).repeat(Math.max(0, Integer.parseInt(tmp.reverse().toString())));
    }

    public static int[] findBuilding(int[] heights) {
        // write code here
        int[] res = new int[heights.length];
        for (int i = 0; i < heights.length; i++) {
//            res[i] = findMaxLength(heights, i);
            res[i] = findBuildingHelper(heights, i);
        }
        return res;
    }

    private static int findBuildingHelper(int[] heights, int line) {
        int amount = 1;
        // from line - 1 to the 0
        int max = 0;
        for (int i = line - 1; i >= 0; i--) {
            if (heights[i] > max) {
                amount++;
                max = heights[i];
            }
        }
        max = 0;
        for (int i = line + 1; i < heights.length; i++) {
            if (heights[i] > max) {
                amount++;
                max = heights[i];
            }
        }
        return amount;
    }

    private static int findMaxLength(int[] heights, int line) {
        int maxIdx = findMaxIdx(heights);
        if (line == maxIdx) return heights.length;
        int left = line, right = line;
        int max = 0;
        for (int i = 0; i < line; i++) {
            if (heights[i] > max) {
                left = i;
                max = heights[i];
            }
        }
        max = 0;
        for (int i = line; i < heights.length; i++) {
            if (heights[i] > max) {
                right = i;
                max = heights[i];
            }
        }
        return right - left + 1;
    }

    private static int findMaxIdx(int[] heights) {
        int idx = 0;
        int max = 0;
        for (int i = 0; i < heights.length; i++) {
            if (heights[i] > max) {
                max = heights[i];
                idx = i;
            }
        }
        return idx;
    }


}
